package org.atf.model.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.JoinColumn;

import org.apache.openjpa.persistence.Externalizer;
import org.apache.openjpa.persistence.Factory;


@Entity
@Table
public class Profile implements Serializable {
	/**
	 * 
	 * 
	 */
	private static final long serialVersionUID = 3779396432315819166L;
	/**
	 * 
	 */

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "PROFILE_ID")
	private Integer id;
	private String region;
	private String profileName;
	private String profileType;
	private String hostname;
	private Integer port;
	private String security;
	private String username;
	@Externalizer("EncryptDecryptHelper.getEncrypted")
	@Factory("EncryptDecryptHelper.getDecrypted")
	private String password;
	private String url;
	@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
	@JoinTable(
	        name = "PROFILE_SERVER",
	        joinColumns = @JoinColumn(name = "PROFILE_ID"),
	        inverseJoinColumns = @JoinColumn(name = "SERVER_ID")
	)
	private  Set<Server> servers = new HashSet<Server>();
	
	
	@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
	@JoinTable(
	        name = "PROFILE_PREFERENCE",
	        joinColumns = @JoinColumn(name = "PROFILE_ID"),
	        inverseJoinColumns = @JoinColumn(name = "PREFERENCE_ID")
	)
	private Set<Preference> preference= new HashSet<Preference>();

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getRegion() {
		return region;
	}
	public void setRegion(String region) {
		this.region = region;
	}
	public String getProfileName() {
		return profileName;
	}
	public void setProfileName(String profileName) {
		this.profileName = profileName;
	}
	public String getProfileType() {
		return profileType;
	}
	public void setProfileType(String profileType) {
		this.profileType = profileType;
	}
	public String getHostname() {
		return hostname;
	}
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}
	public Integer getPort() {
		return port;
	}
	public void setPort(Integer port) {
		this.port = port;
	}
	public String getSecurity() {
		return security;
	}
	public void setSecurity(String security) {
		this.security = security;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
	
	public Set<Server> getServers() {
		return servers;
	}
	public void setServers(Set<Server> servers) {
		this.servers = servers;
	}
	
	public void addServer(Server server){
		if(!servers.contains(server)){
			servers.add(server);
		}
	}
	
	
	public Set<Preference> getPreference() {
		return preference;
	}
	public void setPreference(Set<Preference> preference) {
		this.preference = preference;
	}
	public void addPreference(Preference pref){
		if(!preference.contains(pref)){
			preference.add(pref);
		}
	}
	public void removePreference(Preference pref){
		if(preference.contains(pref)){
			preference.remove(pref);
		}else{
			System.out.println("unable to remove");
		}
	}
	
	public void updateProperties(Profile profile){
		this.region=profile.region;
		this.profileName=profile.profileName;
		this.profileType=profile.profileType;
		this.hostname=profile.hostname;
		this.port=profile.port;
		this.security=profile.security;
		this.username=profile.username;
		this.password=profile.password;
		this.url=profile.url;
		if(profile.getServers() != null && !profile.getServers().isEmpty()){
			this.servers=profile.getServers();
		}
		if(profile.getPreference() != null && !profile.getPreference().isEmpty()){
			this.preference=profile.getPreference();
		}
	}
	@Override
	public String toString() {
		return "Profile [id=" + id + ", region=" + region + ", profileName="
				+ profileName + ", profileType=" + profileType + ", hostname="
				+ hostname + ", port=" + port + ", security=" + security
				+ ", username=" + username + ", url=" + url + ", servers="
				+ servers + ", preference=" + preference + "]";
	}
	
	
	
	
	
	
	
	
	
}
